﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using WeatherReader;
using CommunergyLogic;

public partial class EnergyEntry_electricity : System.Web.UI.Page
{
    ElectricityMeter meter;
    int meterId;
    communergyEntities db = new communergyEntities();

    protected void Page_Load(object sender, EventArgs e)
    {
        var meterIdString = Request.QueryString["meterId"];
        meter = Helpers.GetMeterIfBelongsToUser(db , meterIdString);
        if (meter != null)
        {
            meter.ElectricityDaily_Manual.Load();
            meterId = meter.me_id;
            if (!Page.IsPostBack)
            {
                lblMeterInfo.Text = string.Format("Enter reading for meter {0}", meter.me_meterName);
                UpdateElectricity();
            }
        }
    }

    private void UpdateElectricity()
    {
      
        //WeatherDbDataContext db = new WeatherDbDataContext();
        var electricity = meter.ElectricityDaily_Manual.OrderByDescending(de => de.de_dateTime);
        //dgElectricity.DataSource = electricity;



        DateTime dtmElectricity;
        decimal electricityReading;
        if (electricity.Count() > 0)
        {
            var firstElectricity = electricity.Take(1).FirstOrDefault();
            dtmElectricity = firstElectricity.de_dateTime.AddDays(1);
            electricityReading = firstElectricity.de_kWhTotal.Value;

        }
        else
        {
            dtmElectricity = DateTime.Now.Date;
            electricityReading = 0;
        }
        txtElectricityDate.Text = dtmElectricity.ToString(CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat.ShortDatePattern);
        txtElectricityReading.Text = ((int)electricityReading).ToString();
    }
    protected void btnSubmitElectricity_Click(object sender, EventArgs e)
    {
        DateTime dtReadingDate = DateTime.Parse(txtElectricityDate.Text, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
        decimal electricityReading = decimal.Parse(txtElectricityReading.Text);


        var newReadings = UpdateHelper.CreateElectricityReadings(dtReadingDate, electricityReading, meterId);

        lblElectricityInfo.Text = string.Format("Add {0} records, from {1} to {2}, average value {3}kWh?", newReadings.Count, newReadings.First().de_dateTime.ToLongDateString(), newReadings.Last().de_dateTime.ToLongDateString(), newReadings.First().de_kWhDaily);

        btnConfirmElectricity.Visible = true;
        btnCancelElectricity.Visible = true;
        btnSubmitElectricity.Visible = false;

    }
    protected void btnConfirmElectricity_Click(object sender, EventArgs e)
    {
        DateTime dtReadingDate = DateTime.Parse(txtElectricityDate.Text, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
        decimal electricityReading = decimal.Parse(txtElectricityReading.Text);


        var newReadings = UpdateHelper.CreateElectricityReadings(dtReadingDate, electricityReading, meterId);
        UpdateHelper.SaveNewElectricityReadings(db, meter, newReadings);

        lblElectricityInfo.Text = string.Format("Added {0} records, from {1} to {2}, average value {3}kWh", newReadings.Count, newReadings.First().de_dateTime.ToLongDateString(), newReadings.Last().de_dateTime.ToLongDateString(), newReadings.First().de_kWhDaily);


        UpdateElectricity();
        btnConfirmElectricity.Visible = false;
        btnCancelElectricity.Visible = false;
        btnSubmitElectricity.Visible = true;
    }
    protected void btnCancelElectricity_Click(object sender, EventArgs e)
    {
        btnConfirmElectricity.Visible = false;
        btnCancelElectricity.Visible = false;
        btnSubmitElectricity.Visible = true;
    }


}
